//
//  UIAlertView+DF.m
//  微商社区
//
//  Created by nahuo on 15/1/21.
//  Copyright (c) 2015年 nahuo. All rights reserved.
//

#import "UIAlertView+DF.h"
#import <objc/runtime.h>

@interface UIAlertView() <UIAlertViewDelegate>

@property (nonatomic,copy) void(^clickedButtonBlock)(UIAlertView* alertView,NSInteger buttonIndex);

@end


@implementation UIAlertView (DF)



-(void)showWithBlock:(void (^)(UIAlertView* alertView,NSInteger buttonIndex))block {
    self.clickedButtonBlock = block;
    self.delegate = self;
    [self show];
}

#pragma mark delegate
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if(self.clickedButtonBlock) {
        self.clickedButtonBlock(alertView,buttonIndex);
    }
}

#pragma mark - 运行时相关
static char clickedButtonBlockKey;

-(void)setClickedButtonBlock:(void (^)(UIAlertView *, NSInteger))clickedButtonBlock {
    objc_setAssociatedObject(self, &clickedButtonBlockKey,
                             clickedButtonBlock,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


-(void (^)(UIAlertView *, NSInteger))clickedButtonBlock {
     return objc_getAssociatedObject(self, &clickedButtonBlockKey);
}


+ (void)alertWithTitle:(NSString*)title message:(NSString*)message cancelButtonTitle:(NSString*)cancelButtonTitle {
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
    [alertView show];
}

+ (void)alertTitle:(NSString*)title cancelButtonTitle:(NSString*)cancelButtonTitle {
    [self alertWithTitle:title message:nil cancelButtonTitle:cancelButtonTitle];
}

+ (void)alertMessage:(NSString*)message cancelButtonTitle:(NSString*)cancelButtonTitle {
    [self alertWithTitle:nil message:message cancelButtonTitle:cancelButtonTitle];
}




@end
